/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.StudentDAO;
import com.tces.model.Student;
import com.tces.model.User;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

/**
 *
 * @author arnolda
 */
public class StudentAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(StudentAction.class);
    private User user;
    private List<Student> studentList;
    private String oper, id, studentIDNumber, studentFirstName, studentMiddleName, studentLastName, studentVisible, programCodeRevisionName;

    @Override
    public String execute() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            if (user.getRoleName().equalsIgnoreCase("Administrator") || user.getRoleName().equalsIgnoreCase("Guidance Counseling Head")) {
                studentList = StudentDAO.findAll();
            } else {
                studentList = StudentDAO.findAll(user.getCollegeID());
            }

        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String edit() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            log.info("studentIDNo:"+studentIDNumber);
            if (!Pattern.matches("\\d{2}-\\d{6}", studentIDNumber)) {
                addActionError("Invalid ID Number format.");
                log.error("Invalid ID Number format.");
                return ERROR;
            }
            if (oper.equalsIgnoreCase("add")) {

                if (StudentDAO.add(getStudent()) > 0) {
                    addActionMessage("Successfull insert of student (" + getStudent().getStudentIDNumber() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("edit")) {
                if (StudentDAO.update(getStudent()) > 0) {
                    addActionMessage("Successfull update of student(" + getStudent().getStudentIDNumber() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("del")) {
//                if (InstructorDAO.delete(Integer.parseInt(id)) > 0) {
//                    addActionMessage("Successfull deactivation of instructor[ID:" + id + "]");
//                    return SUCCESS;
//                } else {
//                    addActionError("An error occured. Please contact your System Administrator");
//                    return ERROR;
//                }
            } else {
                addActionMessage("Invalid operation!");
                return ERROR;
            }
            return SUCCESS;
        } catch (Exception ex) {
            addActionError(ex.toString());
            log.error(ex);
            return ERROR;
        }
    }

    private Student getStudent() {
        Student student = new Student();
        if (studentIDNumber != null) {
            student.setStudentIDNumber(studentIDNumber);
        }
        if (studentFirstName != null) {
            student.setStudentFirstName(studentFirstName);
        }
        if (studentMiddleName != null) {
            student.setStudentMiddleName(studentMiddleName);
        }
        if (studentLastName != null) {
            student.setStudentLastName(studentLastName);
        }
        if (studentVisible != null) {
            student.setStudentVisible(Integer.valueOf(studentVisible));
        }
        if (programCodeRevisionName != null) {
            student.setProgramRevisionID(Integer.valueOf(programCodeRevisionName));
        }

        if (id != null && !(id.equalsIgnoreCase("_empty"))) {
            log.info("" + id.equalsIgnoreCase("_empty"));
            student.setStudentID(Integer.parseInt(id));
        }

        return student;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public String getProgramCodeRevisionName() {
        return programCodeRevisionName;
    }

    public void setProgramCodeRevisionName(String programCodeRevisionName) {
        this.programCodeRevisionName = programCodeRevisionName;
    }

    public String getStudentFirstName() {
        return studentFirstName;
    }

    public void setStudentFirstName(String studentFirstName) {
        this.studentFirstName = studentFirstName;
    }

    public String getStudentIDNumber() {
        return studentIDNumber;
    }

    public void setStudentIDNumber(String studentIDNumber) {
        this.studentIDNumber = studentIDNumber;
    }

    public String getStudentLastName() {
        return studentLastName;
    }

    public void setStudentLastName(String studentLastName) {
        this.studentLastName = studentLastName;
    }

    public String getStudentMiddleName() {
        return studentMiddleName;
    }

    public void setStudentMiddleName(String studentMiddleName) {
        this.studentMiddleName = studentMiddleName;
    }

    public String getStudentVisible() {
        return studentVisible;
    }

    public void setStudentVisible(String studentVisible) {
        this.studentVisible = studentVisible;
    }
    
}
